//
//  FankuiViewController.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/8.
//

#import "FankuiViewController.h"

@interface FankuiViewController ()<TZImagePickerControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic , strong) FSTextView * fsTextView;
@property (nonatomic , strong) UILabel * number;
@property (nonatomic , strong) HeaderView * btn;
@property (nonatomic , strong) UIImage * image;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic , strong) SQCustomButton * addImage;
@end

@implementation FankuiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"问题反馈");
}

- (void)SQ_UIViewForSelfView{
    UILabel * label = [[UILabel alloc]init];
    label.text = SQNSLocalString(@"我们重视每个用户的问题反馈，您可以输入您的问题我们会进行核实处理");
    label.numberOfLines = 0;
    label.font = FONT_REGULAR(14);
    label.textColor = SQColor333333;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(26);
        make.right.equalTo(self.view.mas_right).offset(-26);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(10);
    }];
    
    
    UIView * bgView = [[UIView alloc]init];
    bgView .backgroundColor = SQColorF4F4F4;
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 20;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-30);
        make.top.equalTo(label.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(200);
    }];
    [bgView addSubview:self.fsTextView];
    [self.fsTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(bgView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    self.number = [[UILabel alloc]init];
    self.number.textColor = SQColor999999;
    self.number.text = @"0/200";
    self.number.font = FONT_MEDIUM(14);
    [self.view addSubview:self.number];
    [self.number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.top.equalTo(bgView.mas_bottom).offset(10);
    }];
    
    [self.view addSubview:self.addImage];
    [self.addImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(110);
        make.height.mas_equalTo(110);
        make.left.equalTo(self.view).offset(10);
        make.top.equalTo(bgView.mas_bottom).offset(36);
    }];
    UILabel * text = [[UILabel alloc]init];
    text.text = SQNSLocalString(@"点击上传图片");
    text.textColor = SQColor(@"#999999");
    text.font = FONT_MEDIUM(14);
    [self.view addSubview:text];
    [text mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.addImage);
            make.top.equalTo(self.addImage.mas_bottom).offset(8);
    }];
    WS(weakSelf);
    [self.addImage setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_headerButtonClick];
    }];
   
   
    [self.fsTextView addTextDidChangeHandler:^(FSTextView *textView) {
        weakSelf.number.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)textView.text.length];
        if ([textView.formatText isNotEmpty]) {
            weakSelf.btn.colorString = @"#333333";
            weakSelf.btn.backgroundColor = SQColor(@"#4AF8CE");
        } else {
            weakSelf.btn.colorString = @"#999999";
            weakSelf.btn.backgroundColor = SQColor(@"#B6FCEC");
        }
    }];
    
    
    self.btn = [[HeaderView alloc]init];
    self.btn.font = FONT_SEMIBOLD(16);
    self.btn.text = SQNSLocalString(@"提交问题");
    self.btn.colorString = @"#999999";
    self.btn.layer.cornerRadius = 6;
    self.btn.backgroundColor = SQColor(@"#B6FCEC");
    [self.btn.btn addTarget:self action:@selector(SQ_buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-60-FootHeight);
        make.height.mas_equalTo(48);
        make.width.mas_equalTo(SCREEN_WIDETH-32);
        make.centerX.equalTo(self.view);
    }];
    
}
-(void)SQ_buttonClick{
    if (![self.fsTextView.formatText isNotEmpty]) {
        return;
    }
    if (self.image == nil) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请上传图片")];
        return;
    }
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:self.image successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_setUpName:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_setUpName:(NSString*)header{
    if (![header isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"图片上传错误")];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:header forKey:@"img"];
    [dic setObject:self.fsTextView.formatText forKey:@"content"];
    [HttpTool post:@"feedback/save" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"反馈成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self SQ_popViewVC];
        });
       
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)SQ_headerButtonClick{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"上传照片") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"拍照") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_leftButtonClick];
    }];
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"相册") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_rightButtonClick];
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_leftButtonClick{
    WS(weakSelf);
    _imagePickerVc = [[UIImagePickerController alloc] init];
    _imagePickerVc.delegate = self;
    _imagePickerVc.allowsEditing = YES;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        weakSelf.imagePickerVc.sourceType = sourceType;
        [self presentViewController:weakSelf.imagePickerVc animated:YES completion:nil];
    } else {
        NSLog(@"模拟器中无法打开照相机,请在真机中使用");
    }
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    WS(weakSelf);
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *photo = [info objectForKey:UIImagePickerControllerEditedImage];
        if (photo == nil)
            {
                photo = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
        [weakSelf SQ_caijian:photo];
    }else
    {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"相册数据异常")];
    }
}


-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    [self presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf SQ_caijian:photos.firstObject ];
    }];
}

-(void)SQ_caijian:(UIImage*)image {
    self.addImage.SQBackimageView.image = image;
    self.addImage.image = SQGetImage(@"");
    self.image = image;
}


 
- (SQCustomButton *)addImage{
    if (!_addImage) {
        _addImage = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(20, 20) midmargin:0];
        _addImage.image = SQGetImage(@"上传图片");
        _addImage.backgroundColor = SQColorF1F1F1;
        _addImage.layer.cornerRadius = 8;
        _addImage.layer.masksToBounds = YES;
        _addImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
        _addImage.clipsToBounds = YES;
    }
    return _addImage;
}
 
-(FSTextView*)fsTextView{
    if (!_fsTextView) {
        _fsTextView = [FSTextView textView];
        _fsTextView.font = FONT_REGULAR(16);
        _fsTextView.textColor = UIColor.blackColor;
        _fsTextView.placeholder =SQNSLocalString(@"这里输入你要反馈的问题...");
        _fsTextView.placeholderColor = SQColor999999;
        _fsTextView.backgroundColor = UIColor.clearColor;
        _fsTextView.maxLength = 200;
    }
    return _fsTextView;
}
@end
